package cn.bluech.lib.ui.widget.paging;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Description: 分页加载的包装类,占用-9001 -9002两个类型
 *              item项会加1
 * Create by Codeant on 2018/10/26.
 */
public class PagingAdapterWrapper extends RecyclerView.Adapter {
    private static final int VIEW_TYPE_LOADING = -9001;
    private static final int VIEW_TYPE_NON_MORE = -9002;
    private Context context;
    private View loadingView;
    private View nonMoreView;
    private View dummyView;
    private RecyclerView.Adapter adapter;
    private boolean nonMoreData;
    private PagingRecyclerView.IOnLoad loader;

    private Handler handler;

    PagingAdapterWrapper(@NonNull Context context){
        this.context = context;
        dummyView = new View(context);
        dummyView.setLayoutParams(new ViewGroup.LayoutParams(1,1));
        dummyView.setBackgroundColor(Color.TRANSPARENT);
        loadingView = dummyView;
        nonMoreView = dummyView;

        handler = new Handler(Looper.getMainLooper());
    }

    public void setAdapter(RecyclerView.Adapter adapter) {
        this.adapter = adapter;
    }

    public void setLoadingView(View loadingView) {
        this.loadingView = null!=loadingView?loadingView:dummyView;
    }

    public void setNonMoreView(View nonMoreView) {
        this.nonMoreView = null!=nonMoreView?nonMoreView:dummyView;
    }

    /**
     * 设置加载器
     * @param loader 加载器
     */
    public void setLoader(PagingRecyclerView.IOnLoad loader) {
        this.loader = loader;
        nonMoreData = false;
    }

    public void reLoad(){
        nonMoreData = false;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return null!=adapter?adapter.getItemCount()+1:0;
    }

    @Override
    public int getItemViewType(int pos) {
        if (pos == getItemCount() - 1) {
            return nonMoreData?VIEW_TYPE_NON_MORE:VIEW_TYPE_LOADING;
        } else {
            return null!=adapter?adapter.getItemViewType(pos):0;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_LOADING) {
            return new LoadingItemVH(loadingView);
        } else if (viewType == VIEW_TYPE_NON_MORE) {
            return new NoMoreItemVH(nonMoreView);
        } else {
            return adapter.onCreateViewHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int pos) {
        if (holder instanceof LoadingItemVH) {
            if(null!=loader) {
                handler.post(()->{nonMoreData = loader.onLoad(pos);});
            }
        } else if (holder instanceof NoMoreItemVH) {

        } else {
            if(null!=adapter)
                adapter.onBindViewHolder(holder, pos);
        }
    }

    @Override
    public void registerAdapterDataObserver(@NonNull RecyclerView.AdapterDataObserver observer) {
        super.registerAdapterDataObserver(observer);
        adapter.registerAdapterDataObserver(observer);
    }

    @Override
    public void unregisterAdapterDataObserver(@NonNull RecyclerView.AdapterDataObserver observer) {
        super.unregisterAdapterDataObserver(observer);
        adapter.unregisterAdapterDataObserver(observer);
    }

    public static class LoadingItemVH extends RecyclerView.ViewHolder {
        LoadingItemVH(View itemView) {
            super(itemView);
        }
    }

    static class NoMoreItemVH extends RecyclerView.ViewHolder {
        NoMoreItemVH(View itemView) {
            super(itemView);
        }
    }
}
